package com.huoyun.cargo.service;

import java.util.List;

import com.huoyun.base.bean.Page;
import com.huoyun.base.dao.evt.DaoBaseEvt;
import com.huoyun.cargo.bean.CargoInfo;
import com.huoyun.cargo.bean.CargoItem;
import com.huoyun.cargo.dao.CargoInfoDao;
import com.huoyun.cargo.model.CargoUserCompanyModel;
import com.huoyun.cargo.service.evt.GetSimilarCargoByRouteEvt;
import com.huoyun.cargo.service.evt.GetCargoByCityEvt;
import com.huoyun.cargo.service.evt.GetCargosByUserIdEvt;
import com.huoyun.cargo.service.evt.QueryCargoEvt;
import com.huoyun.common.dao.BidInfoDao;

public class CargoServiceImpl implements CargoService {
	private CargoInfoDao cargoInfoDao ;
	private BidInfoDao bidInfoDao ;



	// private TransactionInfoDao transactionInfoDao;
	// private TransportInfoDao transportInfoDao;

	public CargoInfoDao getCargoInfoDao() {
		return cargoInfoDao;
	}

	public void setCargoInfoDao(final CargoInfoDao cargoInfoDao) {
		this.cargoInfoDao = cargoInfoDao;
	}

	/*
	 * public CarRouteDao getCarRouteDao() { return carRouteDao; }
	 * 
	 * 
	 * public void setCarRouteDao(CarRouteDao carRouteDao) { this.carRouteDao =
	 * carRouteDao; }
	 */
	

	/*
	 * public TransactionInfoDao getTransactionInfoDao() { return
	 * transactionInfoDao; }
	 * 
	 * 
	 * public void setTransactionInfoDao(TransactionInfoDao transactionInfoDao)
	 * { this.transactionInfoDao = transactionInfoDao; }
	 * 
	 * 
	 * public TransportInfoDao getTransportInfoDao() { return transportInfoDao;
	 * }
	 * 
	 * 
	 * public void setTransportInfoDao(TransportInfoDao transportInfoDao) {
	 * this.transportInfoDao = transportInfoDao; }
	 */

	
	public long saveCargoInfo(final CargoInfo cargoInfo) 
	{
		return cargoInfoDao.saveCargoInfo(cargoInfo);
	}

	
	public boolean updateCargoInfo(final CargoInfo cargoInfo) {
		cargoInfoDao.updateCargoInfo(cargoInfo);
		return true;
	}

	
	

	
	public CargoInfo getCargoInfo(final CargoInfo cargoInfo) {

		return cargoInfoDao.getCargoInfo(cargoInfo);
	}

	
	public Page getCargosByUserId(GetCargosByUserIdEvt getCargosByUserIdEvt) 
	{
		//构造dao需要的参数
		com.huoyun.cargo.dao.evt.GetCargosByUserIdEvt evt = new com.huoyun.cargo.dao.evt.GetCargosByUserIdEvt();
		evt.setUserId(getCargosByUserIdEvt.getUserId());
		evt.setPageNo(getCargosByUserIdEvt.getPageNo());
		evt.setPageCount(getCargosByUserIdEvt.getPageCount());
		
		//获取订单的总记录数
		long totalRecord = cargoInfoDao.getCargoCountByUserid(getCargosByUserIdEvt.getUserId());
		
		Page page = new Page();
		page.setPageNo(getCargosByUserIdEvt.getPageNo());
		page.setPageCount(getCargosByUserIdEvt.getPageCount());
		
		//设置总记录数
		page.setTotalRecord(totalRecord);
		
		//设置显示的记录列表
		page.setObjectList(cargoInfoDao.getCargosByUserId(evt));
		
		return page;
	}
	/**
	 * 根据城市信息查找货物，用于首页展示
	 * @author yang
	 */
	@Override
	public Page getCargoByCity(GetCargoByCityEvt evt) {
		// TODO Auto-generated method stub
		com.huoyun.cargo.dao.evt.GetCargoByCityEvt daoEvt=new com.huoyun.cargo.dao.evt.GetCargoByCityEvt();
		daoEvt.setProvince(evt.getProvince());
		daoEvt.setCity(evt.getCity());
		daoEvt.setPageCount(evt.getPageCount());
		daoEvt.setPageNo(evt.getPageNo());
		
		Page page=new Page();
		page.setPageNo(evt.getPageNo());
		page.setPageCount(evt.getPageCount());
		//设置总记录数为6
		page.setTotalRecord(6);
		
		page.setObjectList(cargoInfoDao.getCargoByCity(daoEvt));
		return page;
	}
	
	/**
	 * 相关货源查询
	 * @param getSimilarCargoByRouteEvt
	 * @return
	 */
	@Override
	public Page getSimilarCargoByRoute(GetSimilarCargoByRouteEvt getSimilarCargoByRouteEvt){
		com.huoyun.cargo.dao.evt.GetSimilarCargoByRouteEvt evt=new com.huoyun.cargo.dao.evt.GetSimilarCargoByRouteEvt();
		evt.setGoodsId(getSimilarCargoByRouteEvt.getGoodsId());
		evt.setStartCity(getSimilarCargoByRouteEvt.getStartCity());
		evt.setStartProvince(getSimilarCargoByRouteEvt.getStartProvince());
		evt.setDestinationProvince(getSimilarCargoByRouteEvt.getDestinationProvince());
		evt.setDestinationCity(getSimilarCargoByRouteEvt.getDestinationCity());
		
		evt.setPageCount(getSimilarCargoByRouteEvt.getPageCount());
		evt.setPageNo(getSimilarCargoByRouteEvt.getPageNo());
		
		Page page=new Page();
		page.setPageNo(getSimilarCargoByRouteEvt.getPageNo());
		page.setPageCount(getSimilarCargoByRouteEvt.getPageCount());
		page.setTotalRecord(cargoInfoDao.getSimilarCargoCount(evt));
		page.setObjectList(cargoInfoDao.getSimilarCargoByRoute(evt));
		
		return page;
	}
	
	@Override
	public CargoInfo getCargoInfo(long userId, long cargoId)
	{
		return cargoInfoDao.getCargoInfo(userId, cargoId);
	}

	
	public CargoInfo getCargoInfoById(long cargoId) {
		// TODO Auto-generated method stub
		return cargoInfoDao.getCargoInfoById(cargoId);
	}

	  /**
	    * 获取注册货物数量
	    * @param queryCargoEvt
	    * @return
	    */
	   public long queryCargoCount(QueryCargoEvt queryCargoEvt){
		   com.huoyun.cargo.dao.evt.QueryCargoEvt evt = new com.huoyun.cargo.dao.evt.QueryCargoEvt();
		   evt.setAddTime(queryCargoEvt.getAddTime());
		   return cargoInfoDao.queryCargoCount(evt);
	   }

	
	public List<CargoInfo> selectCargos(final CargoInfo cargoInfo) {
		return cargoInfoDao.selectCargosByCondition(cargoInfo);
	}

	
	//车找货
	public Page selectCargosForCar(com.huoyun.cargo.service.evt.SelectCargosForCarEvt searchCriteria) 
	{
		com.huoyun.cargo.dao.evt.SelectCargosForCarEvt searchCriteriaForDao = new com.huoyun.cargo.dao.evt.SelectCargosForCarEvt();
		searchCriteriaForDao.copyData(searchCriteria);
		
		searchCriteriaForDao.setPageCount(searchCriteria.getPageCount());
		searchCriteriaForDao.setPageNo(searchCriteria.getPageNo());
		
		Page page = new Page();
		page.setPageNo(searchCriteria.getPageNo());
		page.setPageCount(searchCriteria.getPageCount());
		page.setTotalRecord(cargoInfoDao.selectCargosForCarCount(searchCriteriaForDao));
		page.setObjectList(cargoInfoDao.selectCargosForCar(searchCriteriaForDao));
		
		return page;
	}

	public void delete(long goodsId) {
		// TODO Auto-generated method stub
		cargoInfoDao.delete(goodsId);
	}

	public List<CargoInfo> selectCargosByCondition(CargoInfo cargoInfo) {
		// TODO Auto-generated method stub
		return cargoInfoDao.selectCargosByCondition(cargoInfo);
	}

	


	@Override
	public CargoItem getCargoItemByGoodsId(long goodsId) {
		// TODO Auto-generated method stub
		return cargoInfoDao.getCargoItemByGoodsId(goodsId);
	}



	public BidInfoDao getBidInfoDao() {
		return bidInfoDao;
	}

	public void setBidInfoDao(BidInfoDao bidInfoDao) {
		this.bidInfoDao = bidInfoDao;
	}

	@Override
	public void updateInvoice(long invoiceId,long cargoId) {
		cargoInfoDao.updateInvoice(invoiceId,cargoId);
	}

	@Override
	public void updateViewCount(int viewCount, long cargoId) {
		cargoInfoDao.updateViewCount(viewCount, cargoId);
	}

	
	/**
	 * 查询最新的N条货物数据
	 * @param topN
	 * @return
	 * @author yang
	 */
	@Override
	public List<CargoUserCompanyModel> getLatestNCargo(long topN){
		return cargoInfoDao.getLatestNCargo(topN);
	}

	@Override
	public Page getAllCargoItem(DaoBaseEvt daoBaseEvt) {
		// TODO Auto-generated method stub
		Page page=new Page();
		page.setPageNo(daoBaseEvt.getPageNo());
		page.setPageCount(daoBaseEvt.getPageCount());
		//设置总记录数为6
		page.setTotalRecord(cargoInfoDao.getAllCount());
		
		page.setObjectList(cargoInfoDao.getAll(daoBaseEvt));
		return page;
	}
	
}
